﻿using System.Threading.Tasks;

namespace BlamanticUI.Components;

/// <summary>
/// Represents a tab with certain <see cref="TabItem"/> components.
/// </summary>
public class Tab : BlazorParentComponentBase<Tab, TabItem>,IHasChildContent
{
    /// <summary>
    /// Initializes a new instance of the <see cref="Tab"/> class.
    /// </summary>
    public Tab()
    {
        VerticalPosition = BlamanticUI.VerticalPosition.Top;
    }
    /// <summary>
    /// Gets or sets the vertical position of tabs.
    /// </summary>
    [Parameter] [CssClass] public VerticalPosition VerticalPosition { get; set; } = VerticalPosition.Top;

    /// <summary>
    /// Gets or sets the color of the background.
    /// </summary>
    [Parameter] public Color? BackgroundColor { get; set; }

    /// <summary>
    /// Gets or sets a value indicating whether switch <see cref="Tab"/> manually.
    /// </summary>
    [Parameter] public bool Manual { get; set; }

    /// <summary>
    /// A callback method when switching tab.
    /// </summary>
    [Parameter] public EventCallback<int> OnSwtich { get; set; }
    /// <summary>
    /// A segment of UI content to render.
    /// </summary>
    [Parameter]public RenderFragment? ChildContent { get; set; }


    ///// <summary>
    ///// Adds the specified component.
    ///// </summary>
    ///// <param name="component">The component.</param>
    //public override void Add(IComponent component)
    //{
    //    ChildComponents.Add(component);
    //    if (ChildComponents.Count == 1)
    //    {
    //        ActivedTabPageIndex = 0;
    //    }
    //    StateHasChanged();
    //}

    /// <summary>
    /// Switches the specified index.
    /// </summary>
    /// <param name="index">The index.</param>
    async Task Switch(int index)
    {
        if (!Manual)
        {
            await Activate(index);
        }
        await OnSwtich.InvokeAsync(index);
        StateHasChanged();
    }

    ///// <summary>
    ///// Switches to specify index of tab.
    ///// </summary>
    ///// <param name="index">The index.</param>
    //public async Task SwitchTo(int index)
    //{
    //    if (index < 0)
    //    {
    //        ActivedTabPageIndex = -1;
    //        return;
    //    }

    //    var activedPage = (TabItem)ChildComponents[index];
    //    ActivedTabPageIndex = index;
    //    await activedPage.OnActived.InvokeAsync(activedPage);
    //}

    protected override void BuildRenderTree(RenderTreeBuilder builder)
    {
        if (VerticalPosition == VerticalPosition.Bottom)
        {
            BuildTabItems(builder);
            BuildTabHeader(builder);
        }
        else
        {

            BuildTabHeader(builder);
            BuildTabItems(builder);
        }
    }

    /// <summary>
    /// Builds the tab items.
    /// </summary>
    /// <param name="builder">The builder.</param>
    private void BuildTabItems(RenderTreeBuilder builder) => builder.CreateCascadingComponent(this, 10, ChildContent);

    /// <summary>
    /// Builds the tab header.
    /// </summary>
    /// <param name="builder">The builder.</param>
    private void BuildTabHeader(RenderTreeBuilder builder)
    {
        builder.CreateComponent<Menu>(0, child =>
        {

            for (int i = 0; i < ChildComponents.Count; i++)
            {

                var item = (TabItem)ChildComponents[i];
                var index = i;

                child.CreateComponent<Item>(0, item.Title, new
                {
                    onclick = EventCallback.Factory.Create<MouseEventArgs>(this, _ => Switch(index)),
                    item.Active
                },
                appendFunc: (b, sequence) =>
                 {
                     b.SetKey(index);
                     return sequence;
                 });
            }
        }, new { Attached = true, AttachedVertical = VerticalPosition, Style = MenuStyle.Tab, Linked = true, BackgroundColor });
    }
}
